package com.resys.server.controller.admin;

import com.resys.entity.dto.CustomerAdminDto;
import com.resys.entity.dto.CustomerDto;
import com.resys.entity.dto.TokenUserInfoDto;
import com.resys.entity.po.Customer;
import com.resys.entity.query.CustomerQuery;
import com.resys.entity.vo.PaginationResultVO;
import com.resys.entity.vo.ResponseVO;
import com.resys.exception.BusinessException;
import com.resys.server.controller.ABaseController;
import com.resys.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController("CustomerController")
@Slf4j
@RequestMapping("/admin/client")
@Api(tags = "管理员模块-管理客户信息接口")
public class CustomerController extends ABaseController {
    @Resource
    private CustomerService customerService;
    @ApiOperation("获取客户列表")
    @GetMapping
    public ResponseVO list(Integer pageNo) {
        CustomerQuery customerQuery = new CustomerQuery();
        customerQuery.setPageNo(pageNo);
        customerQuery.setPageSize(15);
         PaginationResultVO<Customer> list = customerService.findListByPage(customerQuery);
        return getSuccessResponseVO(list);
    }
    @ApiOperation("模糊搜索客户列表")
    @GetMapping("/search")
    public ResponseVO search(String CilentFuzzyName,Integer pageNo) {
        CustomerQuery customerQuery = new CustomerQuery();
        customerQuery.setClientNameFuzzy(CilentFuzzyName);
        customerQuery.setPageNo(pageNo);
        customerQuery.setPageSize(15);
        PaginationResultVO<Customer> list = customerService.findListByPage(customerQuery);
        return getSuccessResponseVO(list);
    }
    @ApiOperation("获取新注册客户账号")
    @GetMapping("/waitPass")
    public ResponseVO waitPass(Integer pageNo) {
        CustomerQuery customerQuery = new CustomerQuery();
        customerQuery.setPageNo(pageNo);
        customerQuery.setAccountStatus(1);
        PaginationResultVO<Customer> list = customerService.findListByPage(customerQuery);
        return getSuccessResponseVO(list);
    }
    @ApiOperation("改变客户状态")
    @PostMapping("/changeStatus")
    public ResponseVO changeStatus(String customerName,Integer status) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo("admin");
       if(tokenUserInfoDto==null){
           throw new BusinessException( "没权限");
       }
        Customer customer = customerService.getCustomerByClientName(customerName);
        if(customer==null){
            throw new RuntimeException("用户不存在");
        }
        customer.setAccountStatus(status);
        customer.setCreateBy(tokenUserInfoDto.getUserId());
        customerService.updateCustomerByCustomerId(customer,customer.getCustomerId());
        return getSuccessResponseVO("修改成功");
    }
    @ApiOperation("修改客户信息")
    @PostMapping("/update")
    public ResponseVO update(@RequestBody CustomerAdminDto customerAdminDto) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo("admin");
        if(tokenUserInfoDto==null){
            throw new BusinessException( "没权限");
        }
         Customer customer = customerService.getCustomerByCustomerId(customerAdminDto.getCustomerId());
        if(customer==null){
            throw new RuntimeException("用户不存在");
        }
        customer.setClientName(customerAdminDto.getClientName());
        customer.setCompany(customerAdminDto.getCompany());
        customer.setRealname(customerAdminDto.getRealname());
        customer.setPhone(customerAdminDto.getPhone());
        customer.setCreateBy(tokenUserInfoDto.getUserId());
        if(customerAdminDto.getPassword()!=null){
            customer.setPassword(DigestUtils.md5DigestAsHex(customerAdminDto.getPassword().getBytes()));
        }
        customerService.updateCustomerByCustomerId(customer,customerAdminDto.getCustomerId());
        return getSuccessResponseVO("修改成功");

    }
    @ApiOperation("删除客户信息")
    @PostMapping("/delete")
    public ResponseVO delete(String customerId) {
        customerService.deleteCustomerByCustomerId(customerId);
        return getSuccessResponseVO("删除成功");
    }
    @ApiOperation("系统异常客户账号强制下线,并发布系统维修公告")
    @GetMapping("/putdown")
    public ResponseVO putdown() {
        cleanTokenCilent();
        return getSuccessResponseVO("强制下线成功");
    }

}
